package net.starwifi.duobao.web;


import net.starwifi.duobao.entity.Setting;
import net.starwifi.duobao.rmi.AppRMIUtils;
import net.starwifi.duobao.service.SettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;


/**
 * Controller - 系统设置
 *
 * @author <a href="http://ruoo.whosenet.com">ruoo.whosenet.com</>
 * @version 1.0
 */
@Controller("adminSettingController")
@RequestMapping("/setting")
public class SettingController extends BaseController{

    @Autowired
    SettingService settingService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public String list(Model model){
        List<Setting> list= settingService.getListData();
        model.addAttribute("list",list );
        return "/setting/list";
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Long id, ModelMap model) {
        Setting entity=settingService.find(id);
        model.addAttribute("entity",entity);
        return "/setting/edit";
    }

    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Setting entity,RedirectAttributes redirectAttributes) {
        if (!isValid(Setting.class, "configValue", entity.getConfigValue())) {
             return ERROR_VIEW;
        }
        settingService.updateEntity(entity,entity.getId());
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list";
    }

    /**
     * 更新
     */
    @RequestMapping(value = "/clearCache", method = RequestMethod.GET)
    public String clearCache(RedirectAttributes redirectAttributes) {
        AppRMIUtils.rmiApi("clearCache","");
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list";
    }
}
